package hongcaosp.app.android.user.qrcode;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.DebugUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.tencent.cos.xml.utils.FileUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import hongcaosp.app.android.R;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.bean.User;
import hongcaosp.app.android.modle.bean.UserInfo;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.DensityUtils;
import xlj.lib.android.base.utils.GalleryUtil;
import xlj.lib.android.base.utils.JsonUtil;
import xlj.lib.android.base.utils.ScreenUtils;
import xlj.lib.android.base.utils.StatusBarUtil;
import xlj.lib.android.base.utils.TaskPool;

public class MineQRCodeActivity extends BaseActivity {
    private ImageView qrCodeIV;
    private TextView nickTV;
    private Bitmap qrBm;

    @Override
    protected void initData(Intent intent) {
        UserInfo userInfo = UserToken.getDefault().getUser();
        nickTV.setText(userInfo.getNickName());
        QRUserBean userBean = new QRUserBean();
        userBean.setUserId(userInfo.getId());
//        userBean.setNickName(userInfo.getNickName());
        final String content = JsonUtil.toJsonStr(userBean);
        Glide.with(this).load(userInfo.getImage()).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                int size = ScreenUtils.getScreenWidth(MineQRCodeActivity.this) - DensityUtils.dip2px(MineQRCodeActivity.this,200);
                Bitmap bitmap = QRCodeUtil.createQRCodeWithLogo(content,size,resource);
                qrBm = bitmap;
                qrCodeIV.setImageBitmap(bitmap);
            }
        });
    }

    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarDarkTheme(this,false);
        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        qrCodeIV = findViewById(R.id.qrcode_iv);
        nickTV = findViewById(R.id.nick);

        findViewById(R.id.save_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TaskPool.myPool().post(new TaskPool.AsyncRunnable<Boolean>() {
                    @Override
                    public void postForeground(Boolean result) {
                        if (result) {
                            ToastManager.getInstance().showToast("保存成功");
                        } else {
                            ToastManager.getInstance().showToast("保存失败");
                        }
                    }

                    @Override
                    public Boolean run() {
                        UserInfo userInfo = UserToken.getDefault().getUser();
//                        try {
////                            saveBitmap(qrBm,"rd_"+ userInfo.getId() + "_qrimage.png");
//
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                            return false;
//                        }
                        return GalleryUtil.saveBmp2Gallery(MineQRCodeActivity.this,qrBm,"rd_"+ userInfo.getId() + "_qrimage.png");
                    }
                });

            }
        });

        findViewById(R.id.scan_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QRScanActivity.toActivity(MineQRCodeActivity.this);
            }
        });
    }

    public void saveBitmap(Bitmap bm,String fileName) throws Exception{
        File f = new File(Environment.getExternalStorageDirectory(), fileName);
//        if (f.exists()) {
//            f.delete();
//        }
        FileOutputStream out = new FileOutputStream(f);
        bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_mineqrcode;
    }
}
